<template>
  <div class="login-container">
    <div class="login-form-wrapper">
      <div class="login-header">
        <h2>欢迎登录</h2>
        <p>体验网易云音乐</p>
      </div>
      <form @submit.prevent="handleLogin" class="login-form">
        <div class="form-group">
          <label for="username">用户名</label>
          <input
            type="text"
            id="username"
            v-model="form.username"
            placeholder="请输入用户名"
            required
          />
        </div>
        <div class="form-group">
          <label for="password">密码</label>
          <input
            type="password"
            id="password"
            v-model="form.password"
            placeholder="请输入密码"
            required
          />
        </div>
        <div class="form-options">
          <label class="checkbox-label">
            <input type="checkbox" v-model="form.rememberMe" />
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-password">忘记密码？</a>
        </div>
        <button type="submit" class="login-btn" :disabled="loading">
          {{ loading ? '登录中...' : '登录' }}
        </button>
        <div class="register-link">
          <span>还没有账号？</span>
          <a href="#" @click.prevent="toggleRegisterMode">立即注册</a>
        </div>
      </form>
      
      <!-- 注册表单 -->
      <form v-if="isRegisterMode" @submit.prevent="handleRegister" class="login-form">
        <div class="form-group">
          <label for="reg-username">用户名</label>
          <input
            type="text"
            id="reg-username"
            v-model="registerForm.username"
            placeholder="请设置用户名"
            required
          />
        </div>
        <div class="form-group">
          <label for="reg-email">邮箱</label>
          <input
            type="email"
            id="reg-email"
            v-model="registerForm.email"
            placeholder="请输入邮箱"
            required
          />
        </div>
        <div class="form-group">
          <label for="reg-password">密码</label>
          <input
            type="password"
            id="reg-password"
            v-model="registerForm.password"
            placeholder="请设置密码"
            required
          />
        </div>
        <button type="submit" class="login-btn" :disabled="regLoading">
          {{ regLoading ? '注册中...' : '注册' }}
        </button>
        <div class="register-link">
          <span>已有账号？</span>
          <a href="#" @click.prevent="toggleRegisterMode">返回登录</a>
        </div>
      </form>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';

const router = useRouter();

// 登录表单数据
const form = ref({
  username: '',
  password: '',
  rememberMe: false
});

// 注册表单数据
const registerForm = ref({
  username: '',
  email: '',
  password: ''
});

// 状态控制
const loading = ref(false);
const regLoading = ref(false);
const isRegisterMode = ref(false);

// 切换登录/注册模式
const toggleRegisterMode = () => {
  isRegisterMode.value = !isRegisterMode.value;
};

// 处理登录
const handleLogin = async () => {
  loading.value = true;
  
  try {
    // 模拟登录请求
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 添加用户名和密码验证
    const correctUsername = 'zhengbofu';
    const correctPassword = 'Zbf102900';
    
    console.log('登录尝试', form.value.username, form.value.password);
    
    if (form.value.username === correctUsername && form.value.password === correctPassword) {
      // 登录成功
      console.log('登录成功', form.value);
      
      // 设置登录状态
      localStorage.setItem('isLoggedIn', 'true');
      
      // 记住用户逻辑
      if (form.value.rememberMe) {
        localStorage.setItem('username', form.value.username);
      } else {
        localStorage.removeItem('username');
      }
      
      // 登录成功后跳转到个人资料页面
      router.push('/profile');
    } else {
      throw new Error('用户名或密码错误');
    }
  } catch (error) {
    console.error('登录失败', error);
    alert('用户名或密码错误，请重试');
  } finally {
    loading.value = false;
  }
};

// 组件挂载时，检查是否有记住的用户名
onMounted(() => {
  const savedUsername = localStorage.getItem('username');
  if (savedUsername) {
    form.value.username = savedUsername;
    form.value.rememberMe = true;
  }
});

// 处理注册
const handleRegister = async () => {
  regLoading.value = true;
  
  try {
    // 模拟注册请求
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 模拟注册成功
    console.log('注册成功', registerForm.value);
    alert('注册成功，请登录');
    
    // 切换回登录模式
    toggleRegisterMode();
  } catch (error) {
    console.error('注册失败', error);
    alert('注册失败，请重试');
  } finally {
    regLoading.value = false;
  }
};
</script>

<style scoped>
.login-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: var(--background-secondary);
  padding: 20px;
}

.login-form-wrapper {
  background-color: var(--background-color);
  border-radius: 8px;
  box-shadow: var(--shadow-medium);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h2 {
  color: var(--text-primary);
  font-size: 24px;
  margin-bottom: 8px;
}

.login-header p {
  color: var(--text-secondary);
  font-size: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-group label {
  color: var(--text-tertiary);
  font-size: 14px;
  font-weight: 500;
}

.form-group input {
  padding: 12px 16px;
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 14px;
  transition: border-color 0.3s ease;
}

.form-group input:focus {
  outline: none;
  border-color: var(--primary-color);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text-secondary);
  font-size: 14px;
  cursor: pointer;
}

.checkbox-label input[type="checkbox"] {
  width: auto;
  cursor: pointer;
}

.forgot-password {
  color: var(--primary-color);
  font-size: 14px;
  text-decoration: none;
  transition: color 0.3s ease;
}

.forgot-password:hover {
  color: #0056b3;
}

.login-btn {
  padding: 12px 20px;
  background-color: var(--primary-color);
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.login-btn:hover:not(:disabled) {
  background-color: #0056b3;
}

.login-btn:disabled {
  background-color: #ccc;
  cursor: not-allowed;
}

.register-link {
  text-align: center;
  margin-top: 10px;
}

.register-link span {
  color: var(--text-secondary);
  font-size: 14px;
}

.register-link a {
  color: var(--primary-color);
  font-size: 14px;
  text-decoration: none;
  margin-left: 4px;
  transition: color 0.3s ease;
}

.register-link a:hover {
  color: #0056b3;
}
</style>